﻿using DatabaseTransfer.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseTransfer.Connections
{
    [Serializable()]
    public class DatabaseConnector : ISerializable
    {
        public string URL { get; set; }

        public string Database { get; set; }

        public string User { get; set; }

        public string Password { get; set; }

        public DatabaseType DatabaseType { get; set; }

        public string Port { get; set; }

        public DatabaseConnector() { }

        public DatabaseConnector(SerializationInfo info, StreamingContext ctxt)
        {
            this.URL = (string)info.GetValue("URL", typeof(string));
            this.Database = (string)info.GetValue("Database", typeof(string));
            this.User = (string)info.GetValue("User", typeof(string));
            this.Password = (string)info.GetValue("Password", typeof(string));
            this.Port = (string)info.GetValue("Port", typeof(string));

            string tmpDBType = (string)info.GetValue("DatabaseType", typeof(string));

            DatabaseType tmpType;

            Enum.TryParse<DatabaseType>(tmpDBType, out tmpType);

            this.DatabaseType = tmpType;

        }

        public static bool operator ==(DatabaseConnector con1, DatabaseConnector con2)
        {
            return con1.URL.Trim().Equals(con2.URL.Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                con1.Database.Trim().Equals(con2.Database.Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                con1.Port.Trim().Equals(con2.Port.Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                con1.DatabaseType == con2.DatabaseType;
        }

        public static bool operator !=(DatabaseConnector con1, DatabaseConnector con2)
        {
            return !(con1 == con2);
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(DatabaseConnector))
            {
                return false;
            }

            DatabaseConnector other = (DatabaseConnector)obj;

            return this.URL.Trim().Equals(other.URL.Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                this.Database.Trim().Equals(other.Database.Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                this.Port.Trim().Equals(other.Port.Trim(), StringComparison.InvariantCultureIgnoreCase) &&
                this.DatabaseType == other.DatabaseType;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() ^ int.Parse(Port);
        }


        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("URL", this.URL);
            info.AddValue("Database", this.Database);
            info.AddValue("User", this.User);
            info.AddValue("Password", this.Password);
            info.AddValue("Port", this.Port);
            info.AddValue("DatabaseType", this.DatabaseType.ToString());

        }
    }
}
